<template>
    <div class="zyglAdd">
<el-form ref="form" :model="form" label-width="80px" :rules="rules">
  <el-form-item label="专业名称" prop="name">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
  <el-form-item label="系" prop="departmentId">
     <el-select filterable v-model="form.departmentId" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.name"
      :value="item.id">
    </el-option>
  </el-select>
  </el-form-item>
   
  <el-form-item>
    <el-button type="primary" @click="onSubmit">立即创建</el-button>
    <el-button @click="handleClose">取消</el-button>
  </el-form-item>
</el-form>
    </div>
</template>

<script>
import {zyglAdd,departmentAll} from "@/api/zygl"
  export default {
    data() {
      return {
        form: {
          name: '',
          id:-1,
          departmentId :"",
        },
        rules: {
                name: [
                    { required: true, message: '请输入专业名称', trigger: 'blur' },
                ],
                departmentId: [
                    { required: true, message: '请选择系名称', trigger: 'blur' },
                ],
            },
            options:[{

            }]
      }
    },
    methods: {
      onSubmit() {
        console.log('submit!');
         this.$refs['form'].validate(flag => {
            if(flag){
                console.log(this.form);
                zyglAdd(this.form).then(res=>{
                  console.log(res);
                  if(res.code===0){
                    this.$message({
                      message:"添加成功",
                      type:"success",
                      duration:1000
                    })
                    this.$emit("done")
                  }
                  
                })
                
            }
         })
      },
      handleClose(){
        this.$emit("close")
      },
      //获取系列表
      getList(){
        departmentAll().then(res=>{
          console.log(res);
          if(res.code===0){
            this.options=res.data
          }
        })
      }
    },
    mounted(){
      this.getList()
    }

  }
</script>